﻿///////////////////////////////////////////////////////////////////////////////
// File:        RealmList.cc
// Author:      671643387@qq.com
// Date:        2016年1月18日 上午11:18:46
// Description:
///////////////////////////////////////////////////////////////////////////////

#include "Log.h"
#include "Database.h"
#include "RealmList.h"

extern __MWOW_DB_ gLoginDB;

#define REALM_UPDATE_INTERVAL   60000

RealmList::RealmList(void)
    : updateTime_(0)
{

}

RealmList::~RealmList(void)
{

}

void RealmList::Update(void)
{
    ::mki::yxs::util::TIME_t now = ::mki::yxs::util::FromStartup();
    if ((now - updateTime_) < REALM_UPDATE_INTERVAL)
        return;

    __YXS_LOCK_(updateLock_);

    updateTime_ = now;
    realms_.clear();

    MWOW_DEBUG << "load realms from database";

    __YXS_SQLRESULTSET_PTR_ result = gLoginDB.Query("SELECT * FROM realmlist;");
    if (result)
    {
        do 
        {
            struct Realm realm = {0};
            realm.id = (*result)[0]->GetUInt32();
            realm.name = (*result)[1]->GetString();
            realm.address.append((*result)[2]->GetCString())
                .append(":").append((*result)[3]->GetCString());
            realm.icon = (*result)[4]->GetUInt8();
            realm.realmflags = (eRealmFlags)(*result)[5]->GetUInt8();
            realm.timezone = (*result)[6]->GetUInt8();
            realm.populationLevel = (*result)[8]->GetFloat();
            realms_.push_back(realm);
        } while (result->NextRow());
    }
}